<?php
/* 设置报错等级 */
error_reporting(E_ALL ^ E_NOTICE);

/* 定义默认HTTP头部 */
header('content-type: text/html; charset=utf-8');
header('Expires: Fri, 14 Mar 1980 20:53:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');

setlocale(LC_ALL, 'zh_CN.UTF-8');

session_start();

if (__FILE__ == '') {
    die('Fatal error code: 0');
}

/* 定义ROOT_PATH，之后所有的include以ROOT_PATH为基准 */
define('ROOT_PATH', str_replace('includes/init.php', '', str_replace('\\', '/', __FILE__)));
$ROOT_PATH = ROOT_PATH;

$WEB_ROOT = substr(realpath(dirname(__FILE__).'/../'), strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
if (trim($WEB_ROOT, '/\\')) {
	$WEB_ROOT = '/'.trim($WEB_ROOT, '/\\').'/';
} else {
	$WEB_ROOT = '/';
}
$WEB_ROOT = str_replace("\\", "/", $WEB_ROOT);
define('WEB_ROOT', $WEB_ROOT);

/* 初始化设置 */
@ini_set('session.cache_expire',  180);
@ini_set('session.use_trans_sid', 0);
@ini_set('session.use_cookies',   1);
@ini_set('session.auto_start',    0);
@ini_set('display_errors',        1);
if (DIRECTORY_SEPARATOR == '\\') {
    @ini_set('include_path',      '.;' . ROOT_PATH);
} else {
    @ini_set('include_path',      '.:' . ROOT_PATH);
}

/* 引入公用类库 */
require_once(ROOT_PATH . 'includes/lib_common.php');
require_once(ROOT_PATH . 'includes/config.php');

if (PHP_VERSION >= '5.1' && !empty($timezone)) {
    date_default_timezone_set($timezone);
}

/* 对用户传入的变量进行转义操作。*/
if (!get_magic_quotes_gpc()) {
    if (!empty($_GET)) {
        $_GET  = addslashes_deep($_GET);
    }
    if (!empty($_POST)) {
        $_POST = addslashes_deep($_POST);
    }

    $_COOKIE   = addslashes_deep($_COOKIE);
    $_REQUEST  = addslashes_deep($_REQUEST);
}


/* 初始化数据库类，目前只支持mysql类库 */
if ($init_db_type == '') {
	$init_db_type = 'mysql';
}
require(ROOT_PATH . "includes/cls_{$init_db_type}.php");
$db_class = new ReflectionClass("cls_{$init_db_type}");
$db = $db_class->newInstanceArgs(array($db_conf['default']));

/* 设置compiled文件夹的权限 */
if (!file_exists(ROOT_PATH . 'compiled/')) {
    @mkdir(ROOT_PATH . 'compiled', 0777);
    @chmod(ROOT_PATH . 'compiled', 0777);
}
clearstatcache();

/* 定义默认的模板名 */
define(TEMPLATE_NAME, 'default');

/* 创建 Smarty 对象。*/
require(ROOT_PATH . 'includes/smarty/Smarty.class.php');
$smarty = new Smarty;
$smarty->setTemplateDir(ROOT_PATH . 'templates');
$smarty->setCompileDir(ROOT_PATH . 'compiled');
$smarty->setPluginsDir(ROOT_PATH . 'includes/smarty/plugins');
$smarty->caching = false;

/* 为模板设置全局变量 */
$smarty->assign('WEB_ROOT', $WEB_ROOT);
$smarty->assign('_CFG', $_CFG);
?>